package com.doform.android.xianggucheng.activity;

import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.adapter.CommonListAdapter;
import com.doform.android.xianggucheng.bean.ClientBean;
import com.doform.android.xianggucheng.bean.ClientCategoryQueryBean;
import com.doform.android.xianggucheng.bean.ClientDetail;
import com.doform.android.xianggucheng.bean.StarQueryBean;
import com.doform.android.xianggucheng.bean.request_bean.ReqQueryClientCategory;
import com.doform.android.xianggucheng.holder.ViewHolder;
import com.doform.android.xianggucheng.popwindow.IPopWindow;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.utils.Constans;
import com.doform.android.xianggucheng.utils.RexUtils;
import com.doform.android.xianggucheng.utils.TsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * Created by Administrator on 2017/5/12.
 */

public class UpdateClientActivity extends BaseTitleActivity {

    @BindView(R.id.rl1)
    RelativeLayout mLl;

    @BindView(R.id.et_name)
    EditText et_name;

    @BindView(R.id.et_person)
    EditText et_person;

    @BindView(R.id.et_phone)
    EditText et_phone;

    @BindView(R.id.et_detail_address)
    EditText et_detail_address;

    @BindView(R.id.tv_category)
    TextView tv_category;

    @BindView(R.id.tv_star)
    TextView tv_star;

    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;


    private List<StarQueryBean> mStarData = new ArrayList<>();
    private List<ClientCategoryQueryBean> mCategoryData = new ArrayList<>();

    private IPopWindow mStarPopupWindow, mCategoryPopupWindow;
    private CommonListAdapter<StarQueryBean> mStarCommonAdapter;
    private CommonListAdapter<ClientCategoryQueryBean> mCategoryAdapter;

    private ClientBean.ListBean mBean;

    private String mCategoryName = "";
    private String mStarName = "";
    private String mStarId = "";
    private String mCategoryId = "";

    @Override
    protected int layoutResId() {
        return R.layout.activity_new_and_update_client;
    }

    @Override
    protected void init() {
        if (mStarPopupWindow == null) {
            mStarPopupWindow = new IPopWindow(this, R.layout.pop_new_add_goodsk);
        }

        if (mCategoryPopupWindow == null) {
            mCategoryPopupWindow = new IPopWindow(this, R.layout.pop_new_add_goodsk);
        }
        ClientBean.ListBean bean = (ClientBean.ListBean) getIntent().getSerializableExtra(Constans.EXTRA_UPDATE_CLENT);
        this.mBean = bean;

    }

    @Override
    protected void initData() {
        getDataForServiceStar();
        getDataForServiceCategory();
    }

    @Override
    protected void updateData() {
        super.updateData();
        getDataForService();
    }

    private void getDataForService() {

        HttpManager.instance()
                .getClientDetail(mBean.getId(), mBean.getGuid())
                .subscribe(new ErrorHandleSubscriber<ArrayList<ClientDetail>>() {
                    @Override
                    public void onNext(@NonNull ArrayList<ClientDetail> clientDetail) {
                        parseData(clientDetail.get(0));
                    }
                });
    }

    private void parseData(ClientDetail mMessBean) {

        et_name.setText(mMessBean.getName());
        et_person.setText(mMessBean.getLnkname());
        et_detail_address.setText(mMessBean.getAddr());
        et_phone.setText(mMessBean.getMobile());

        et_phone.requestFocus();
        et_detail_address.requestFocus();
        et_person.requestFocus();
        et_name.requestFocus();

        mStarId = mMessBean.getStarid();
        mStarName = mMessBean.getSnama();

        mCategoryId = mMessBean.getBizclsid();
        mCategoryName = mMessBean.getBizclsnickname();

        tv_star.setText(mStarName);
        tv_category.setText(mCategoryName);

    }

    @Override
    protected void initListener() {
        initStarAdater();
        initCategoryAdapter();
    }

    private void initCategoryAdapter() {
        mCategoryPopupWindow.setHeadName("用户分类");

        mCategoryAdapter = new CommonListAdapter<ClientCategoryQueryBean>(this, mCategoryData, R.layout.pop_item) {
            @Override
            protected void convert(ViewHolder vh, final List<ClientCategoryQueryBean> datas, final int position) {

                final ClientCategoryQueryBean bean = datas.get(position);

                TextView tv = vh.getView(R.id.tv);
                tv.setText(bean.getName());

                vh.getView(R.id.rl_select).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mCategoryName = bean.getName();
                        mCategoryId = bean.getId();
                        tv_category.setText(mCategoryName);
                        mCategoryPopupWindow.dismiss();
                    }
                });
            }
        };

        mCategoryPopupWindow.setPopAdapter(mCategoryAdapter);
    }

    private void initStarAdater() {

        mStarPopupWindow.setHeadName("用户星级");

        mStarCommonAdapter = new CommonListAdapter<StarQueryBean>(this, mStarData, R.layout.pop_item) {
            @Override
            protected void convert(ViewHolder vh, final List<StarQueryBean> datas, final int position) {

                final StarQueryBean bean = datas.get(position);

                TextView tv = vh.getView(R.id.tv);
                tv.setText(bean.getName());

                vh.getView(R.id.rl_select).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mStarName = bean.getName();
                        mStarId = bean.getId();
                        tv_star.setText(mStarName);
                        mStarPopupWindow.dismiss();
                    }
                });
            }
        };

        mStarPopupWindow.setPopAdapter(mStarCommonAdapter);
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle("修改客户");
        et_name.setHint(getResources().getString(R.string.client_name));
        et_person.setHint(getResources().getString(R.string.client_person));
        et_phone.setHint(getResources().getString(R.string.client_phone));
        et_detail_address.setHint(getResources().getString(R.string.client_addr));
    }

    private void beforeSubmit() {

        String mName = et_name.getText().toString().trim();
        String mPerson = et_person.getText().toString().trim();
        String mPhone = et_phone.getText().toString().trim();
        String mDetailAddress = et_detail_address.getText().toString().trim();

        if (TextUtils.isEmpty(mName)) {
            TsUtils.showToast("名称不能为空");
            return;
        } else if (TextUtils.isEmpty(mPerson)) {
            TsUtils.showToast("联系人不能为空");
            return;
        } else if (TextUtils.isEmpty(mPhone)) {
            TsUtils.showToast("联系电话不能为空");
            return;
        } else if (TextUtils.isEmpty(mPhone) || RexUtils.isMobileNO(mPhone)) {
            TsUtils.showToast("联系电话格式不正确");
            return;
        } else if (TextUtils.isEmpty(mDetailAddress)) {
            TsUtils.showToast("详细地址不能为空");
            return;
        }
        postDataToService(mName, mPerson, mPhone, mDetailAddress, mCategoryId, mCategoryName, mStarId);
    }

    private void postDataToService(String mName, String mPerson, String mPhone, String mDetailAddress,
                                   String mCategoryId, String mCategoryName, String mStarId) {
        HttpManager.instance()
                .updateClient(mName, mPerson, mPhone, mDetailAddress, mCategoryId, mCategoryName, mStarId, mBean.getId(), mBean.getGuid())
                .subscribe(new ErrorHandleSubscriber<String>() {
                    @Override
                    public void onNext(@NonNull String str) {
                        finish();
                    }
                });
    }

    //    获取分类数据
    private void getDataForServiceCategory() {
        HttpManager.instance()
                .getClientCategoryQuery2(new ReqQueryClientCategory())
                .subscribe(new ErrorHandleSubscriber<ArrayList<ClientCategoryQueryBean>>() {
                    @Override
                    public void onNext(@NonNull ArrayList<ClientCategoryQueryBean> clientCategoryBean) {
                        parseCategoryData(clientCategoryBean);
                    }
                });
    }

    //    刷新客户分类数据
    private void parseCategoryData(List<ClientCategoryQueryBean> data) {
        mCategoryData.clear();
        mCategoryData.addAll(data);
        if (mCategoryAdapter != null) {
            mCategoryAdapter.notifyDataSetChanged();
        }
    }

    //    获取星级数据
    private void getDataForServiceStar() {
        HttpManager.instance()
                .getStarQuery2()
                .subscribe(new ErrorHandleSubscriber<ArrayList<StarQueryBean>>() {
                    @Override
                    public void onNext(@NonNull ArrayList<StarQueryBean> starBean) {
                        jsonData(starBean);
                    }
                });
    }

    //    解析数据
    private void jsonData(List<StarQueryBean> data) {
        mStarData.clear();
        mStarData.addAll(data);
        if (mStarCommonAdapter != null) {
            mStarCommonAdapter.notifyDataSetChanged();
        }
    }

    @OnClick({R.id.btn_submit, R.id.tv_star, R.id.tv_category})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                beforeSubmit();
                break;
            case R.id.tv_star:
                mStarPopupWindow.showAtLocation(mLl, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.tv_category:
                mCategoryPopupWindow.showAtLocation(mLl, Gravity.BOTTOM, 0, 0);
                break;
            default:
                break;
        }
    }
}
